package com.phoenix.user.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.phoenix.core.constant.ConstantsCode;
import com.phoenix.core.exception.BizRuntimeException;
import com.phoenix.user.entity.User;
import com.phoenix.user.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MovieController {
  @Autowired
  private UserFeignClient userFeignClient;

  @HystrixCommand(fallbackMethod="findByIdFallback")
  @GetMapping("/users/{id}")
  public User findById(@PathVariable Long id) {

      User user=this.userFeignClient.findById(id);
      if(user==null){
          throw new BizRuntimeException(
                  ConstantsCode.DATA_NOT_FOUND,
                  "我的错误",
                  "我的错误");
      }

      return user;
  }

  @GetMapping("/users")
  public List<User> findAll(){
    return  this.userFeignClient.findAll();
  }

  public User findByIdFallback(Long id){
        User user=new User();
        user.setId(-1L);
        user.setName("默认用户");
        return user;
  }
}
